<?php

/**
 * FootballFactory, Copyright Robin Loop & Pascal Wirz, 2014
 * ---
 * Stellt alle Funktionen zum Auflisten und Editieren der Spielorte zur Verfügung
 *
 * @author Robin
 */

include "redirector.php";
include "../ff_includes/class_database.php";
include "../ff_includes/class_gamevenue.php";

$action = isset( $_GET['action'] ) ? $_GET['action'] : "";

switch ( $action ) {
  case 'list':
    listGamevenues();
    break;
  case 'new':
    newGamevenue();
    break;
  case 'edit':
    editGamevenue();
    break;
  case 'save':
    saveGamevenue();
    break;
  case 'delete':
    deleteGamevenue();
    break;
  default:
    listGamevenue();
}

function listGamevenues() {
    $db = new Database();
    $gamevenueList = Gamevenue::getGamevenueList();
    $numbGamevenues = $gamevenueList['totalRows'];
    $gamevenues = $gamevenueList['gamevenueList'];
    
    require("templates/gamevenue_list.php");
}

function newGamevenue() {
    $db = new Database();
    $gamevenue = new Gamevenue();
    
    require("templates/gamevenue_edit.php");
}

function editGamevenue() {
    $db = new Database();
    $gamevenueId = (int)$_GET['gamevenueId'];
    $gamevenue = Gamevenue::getGamevenueById($gamevenueId);
    
    require("templates/gamevenue_edit.php");
}

function saveGamevenue() {
    // speichern
    if ( isset( $_POST['saveChanges'] ) ) {
        $gamevenue = new Gamevenue($_POST);
        // update
        if ($gamevenue->getId() != 0) {
            $gamevenue->update();
        }
        // neuer Eintrag
        else {
            $gamevenue->insert();
        }
        header( "Location: gamevenue.php?action=list" );
    }
    // abbrechen
    elseif ( isset( $_POST['cancel'] ) ) {
        header( "Location: gamevenue.php?action=list" );
    }
}

function deleteGamevenue() {
    $gamevenue = Gamevenue::getGamevenueById((int)$_GET['gamevenueId']);
    $gamevenue->delete();
    header( "Location: gamevenue.php?action=list" );
}
?>
